//
//  YTGetNewData.m
//  Team9Project
//
//  Created by dlios on 15/10/21.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "YTGetNewData.h"
#import <MJRefresh.h>
#import "JQAFNetworkTool.h"
#import "Team9MainDisscussModel.h"
@implementation YTGetNewData

+ (void) creatNewData:(UITableView *)tableView
               newurl:(NSString *)newUrl
              moreurl:(NSString *)moreUrl
                array:(NSMutableArray *)arr{

    // 触发下拉刷新方法
    tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [[self class] loadNewData:tableView newurl:newUrl array:arr];
        
    }];
    
    
    // 触发上拉加载数据方法
    tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [[self class] loadMoreData:tableView moreurl:moreUrl array:arr];
        
    }];
    
    
    
    [tableView reloadData];
    

}


// 刷新
+ (void) loadNewData:(UITableView *)tableView
                 newurl:(NSString *)newUrl
               array:(NSMutableArray *)arr{

    [JQAFNetworkTool getUrl:newUrl body:nil result:JQJSON headerFile:nil success:^(id result) {
        
    
        [arr addObject: [Team9MainDisscussModel baseModelByArr:result[@"data"][@"topic_list"][@"topics"]].mutableCopy];
        
        [tableView reloadData];
        [tableView.header endRefreshing];
        
    } failure:^(NSError *error) {
        
    }];

    
}



// 加载
+ (void) loadMoreData:(UITableView *)tableView
                 moreurl:(NSString *)moreUrl
                array:(NSMutableArray *)arr{

 static NSInteger i = 2;
    
    
    [JQAFNetworkTool getUrl:[NSString stringWithFormat:moreUrl, i] body:nil result:JQJSON headerFile:nil success:^(id result) {
//        NSLog(@"网址 : %@", [NSString stringWithFormat:moreUrl, i]);
        
      
        NSMutableArray *newArr = [Team9MainDisscussModel baseModelByArr:result[@"data"][@"topic_list"][@"topics"]].mutableCopy;
        
        
        if (arr.count == 0) {
            
            [tableView.footer endRefreshingWithNoMoreData];
            
        } else {
            
            [tableView.footer endRefreshing];
            
        }
        [arr addObjectsFromArray:newArr];
        
        [tableView reloadData];
        
        i+= 1;
        
        
    } failure:^(NSError *error) {
        [tableView.footer endRefreshingWithNoMoreData];
    }];
    

    
    
}
@end
